package com.yw.controller;


import com.yw.directory.domain.Directory;
import com.yw.directory.dto.ChildDTO;
import com.yw.directory.vo.DirectoryVO;
import com.yw.result.R;
import com.yw.service.DirectoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author taj
 * @version 1.0
 */
@Slf4j
@Tag(name = "总目录管理",description = "总目录")
@RestController
@RequestMapping("/directory")
public class DirectoryController {

    @Resource
    private DirectoryService directoryService;

    /**
     * 根据目录id查询对应目录
     * @param id
     * @return
     */
    @Operation(summary = "根据目录id查询对应目录信息",description = "根据目录id查询对应目录信息")
    @PostMapping("/getContents")
    public R getContents(@RequestParam Integer id){
        DirectoryVO directoryVOList = directoryService.getContents(id);
        return R.OK(directoryVOList);
    }

    //新增一级目录
    @Operation(summary = "新增一级目录")
    @PostMapping("/addContentTop")
    public R addContentTop(@RequestParam String name){
        Boolean result = directoryService.addTop(name);
        return result ? R.OK("新增目录成功") : R.FAIL("新增目录失败");
    }

    //新增叶子目录
    @Operation(summary = "新增叶子目录")
    @PostMapping("/addContentChild")
    public R addContentChild(@RequestBody Directory directory){
        Boolean result = directoryService.addChild(directory);
        return result ? R.OK("新增下一级目录成功") : R.FAIL("新增下一级目录失败");
    }

    //删除叶子目录
    @Operation(summary = "删除叶子目录")
    @PostMapping("/delContentChild")
    public R delContentChild(Integer id){
        Boolean result = directoryService.delChild(id);
        return result ? R.OK("删除目录成功") : R.FAIL("删除目录失败");
    }

    //修改叶子目录名称
    @Operation(summary = "修改叶子目录")
    @PostMapping("/updateContentChild")
    public R updateContentChild(@RequestBody Directory directory){
        Boolean result = directoryService.updateChild(directory);
        return result ? R.OK("修改目录名称成功") : R.FAIL("修改目录名称失败");
    }

    //获取任意父级目录的叶子节点
    @Operation(summary = "获取任意父级目录的叶子节点")
    @PostMapping("/getChildList")
    public R getChildList(@RequestParam Integer id){
        List<ChildDTO> childDTOList = directoryService.getChildList(id,null);
        return R.OK(childDTOList);
    }
}

